import AudioManager from "./AudioManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PopUp extends cc.Component {

    @property(AudioManager)
    audio: AudioManager = null;

    _touchable = false;

    protected onEnable(): void {
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    }

    protected onDisable(): void {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    }

    private onTouchStart() {
        this.hidePopUp();
    }

    private _showEndCallback: () => void = null;
    showPopUp(callback?: () => void) {
        this._showEndCallback = callback;
        this.node.active = true;
        this.getComponent(cc.Animation).play('popup-show');
    }

    hidePopUp() {
        if (!this._touchable) return;

        this.audio.playCursor();
        this.getComponent(cc.Animation).play('popup-hide');
    }

    frameEvent_OnShow() {
        this._touchable = true;
        if (this._showEndCallback) {
            this._showEndCallback();
        }
    }

    frameEvent_OnHide() {
        this.node.active = false;
        this._touchable = false;
    }
}
